package com.smartlifev30.utils;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * @author tys
 * @date 2018/8/29
 */

public class SharedPreferencesUtil {

    /**
     * sp文件名
     */
    private static final String SHARED_PREFERENCES_NAME = "Setting";

    /**
     * 当前登陆app的versionCode  (key)
     */
    public static final String VERSION_CODE = "version_code";

    /**
     * 服务器token (key)
     */
    public static final String REMOTE_TOKEN = "remote_token";

    /**
     * 登陆账号名 (key)
     */
    public static final String USER_NAME = "user_name";

    /**
     * 登陆密码 (key)
     */
    public static final String USER_PWD = "user_pwd";

    /**
     * 服务器IP地址 (key)
     */
    public static final String SERVER_IP = "server_ip";

    /**
     * 服务器端口 (key)
     */
    public static final String SERVER_PORT = "server_port";
    private static SharedPreferences sharedPreferences;


    public static String getString(Context context, String name) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(name, "");
    }

    public static void putString(Context context, String name, String val) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putString(name, val).apply();
    }

    public static int getInt(Context context, String name) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getInt(name, -1);
    }

    public static void putInt(Context context, String name, int val) {
        SharedPreferences sp = getSharedPreferences(context);
        sp.edit().putInt(name, val).apply();
    }


    private static SharedPreferences getSharedPreferences(Context context) {
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(
                    SharedPreferencesUtil.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
        }
        return sharedPreferences;
    }


}
